package com.powernode.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.powernode.config.WhiteUrlsConfig;
import com.powernode.constant.AuthConstants;
import com.powernode.constant.BusinessEnum;
import com.powernode.constant.HttpConstants;
import com.powernode.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * 全局过滤器 请求头的Authorization bearer token
 * @Author: DongGuoZhen
 * @Date: 2025/09/11/13:46
 * @Description:
 */
@Component
@Slf4j
public class AuthFilter implements GlobalFilter, Ordered {

    @Autowired
    private WhiteUrlsConfig whiteUrlsConfig;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 校验token
     * 1. 获取请求路径
     * 2. 判断请求路径是否可以放行
     *     放行：不需要验证身份  不放行：进行身份认证
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        获取请求队列
        ServerHttpRequest request = exchange.getRequest();
        String path = request.getPath().toString();
//        判断当前请求是否进行放行  是否存在于白名单中
        if(whiteUrlsConfig.getAllowUrls().contains(path)) {
//            包含在白名单中  需要放行
            return chain.filter(exchange);
        }
//        请求路径不包含在白名单中 需要进行身份认证
//        从请求头中获取Authorization的值，格式为bearer token
        String authorizationValue = request.getHeaders().getFirst(AuthConstants.AUTHORIZATION);
        if(StringUtils.hasText(authorizationValue)) {
            String tokenValue = authorizationValue.replaceFirst(AuthConstants.BEARER, "");
//            判断token值是否有值且是否在redis中存在
            if(StringUtils.hasText(tokenValue) &&
                    stringRedisTemplate.hasKey(AuthConstants.LOGIN_TOKEN_PREFIX + tokenValue)) {

                return chain.filter(exchange);
            }
        }
//        验证身份没有通过 或 请求不合法
        log.error("拦截非法请求，时间{}，请求API路径{}",new Date(),path);
//        获取响应对象
        ServerHttpResponse response = exchange.getResponse();
        response.getHeaders().set(HttpConstants.CONTENT_TYPE, HttpConstants.APPLICATION_JSON);
        // 设置响应消息
        Result<Object> result = Result.fail(BusinessEnum.UN_AUTHORIZATION);
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] bytes = null;
        try {
            bytes = objectMapper.writeValueAsBytes(result);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        DataBuffer dataBuffer = response.bufferFactory().wrap(bytes);

        return response.writeWith(Mono.just(dataBuffer));
    }

    @Override
    public int getOrder() {
        return -5;
    }
}
